/*
 * Created by JFormDesigner on Sun Dec 08 14:56:08 CST 2024
 */

package com.hafu.view.ShangPin;

import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;

/**
 * @author shb
 */
public class AddShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();
    public AddShangPinFrm() {
        initComponents();
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 添加商品
        String name = textField4.getText();
        if(StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"商品名字不能为空");
            return;
        }
        if (StringUtil.isEmpty(textField5.getText())){
            JOptionPane.showMessageDialog(null,"商品进价不能为空");
            return;
        }
        if (StringUtil.isEmpty(textField6.getText())){
            JOptionPane.showMessageDialog(null,"商品售价不能为空");
            return;
        }
        BigDecimal price = new BigDecimal(textField5.getText());
        BigDecimal sell_Price = new BigDecimal(textField6.getText());
        SpInfo spInfo = new SpInfo(name,price,sell_Price);
        Boolean aBoolean = spInfoService.addSpInfo(spInfo);
        if(aBoolean){
            JOptionPane.showMessageDialog(null,"添加成功");
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null,"添加失败");
        }



    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        textField5 = new JTextField();
        textField6 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5546\u54c1");
        Container contentPane = getContentPane();

        //---- textField1 ----
        textField1.setText("\u5546\u54c1\u540d\u5b57");

        //---- textField2 ----
        textField2.setText("\u5546\u54c1\u8fdb\u4ef7");

        //---- textField3 ----
        textField3.setText("\u5546\u54c1\u552e\u4ef7");

        //---- button1 ----
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> button1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(16, 16, 16)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGap(115, 115, 115)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField4, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                .addComponent(textField5, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                .addComponent(textField6, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(143, 143, 143)
                            .addComponent(button1)))
                    .addContainerGap(57, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(33, 33, 33)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(35, 35, 35)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addGap(22, 22, 22))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
